package com.example.domain.repository;

import com.example.domain.entity.ThresholdDiscountRule;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.List;
import java.util.Optional;

/**
 * 满减规则Repository
 * 
 * @author Means
 * @since 2025-10-27
 */
@Repository
public interface ThresholdDiscountRuleRepository extends JpaRepository<ThresholdDiscountRule, String> {
    
    // ========== 基础查询 ==========
    
    /**
     * 查询未删除的满减规则列表
     * 
     * @param isDeleted 是否删除
     * @return 满减规则列表
     */
    List<ThresholdDiscountRule> findByIsDeleted(Boolean isDeleted);
    
    /**
     * 根据规则ID查询未删除的满减规则
     * 
     * @param ruleId 规则ID
     * @param isDeleted 是否删除
     * @return 满减规则列表
     */
    List<ThresholdDiscountRule> findByRuleIdAndIsDeleted(String ruleId, Boolean isDeleted);
    
    /**
     * 根据规则ID查询未删除的满减规则（单个）
     * 
     * @param ruleId 规则ID
     * @param isDeleted 是否删除
     * @return 满减规则对象
     */
    Optional<ThresholdDiscountRule> findFirstByRuleIdAndIsDeleted(String ruleId, Boolean isDeleted);
    
    /**
     * 根据是否可叠加查询未删除的满减规则
     * 
     * @param stackable 是否可叠加
     * @param isDeleted 是否删除
     * @return 满减规则列表
     */
    List<ThresholdDiscountRule> findByStackableAndIsDeleted(Boolean stackable, Boolean isDeleted);
    
    // ========== 批量查询 ==========
    
    /**
     * 根据规则ID列表批量查询满减规则
     * 
     * @param ruleIds 规则ID列表
     * @param isDeleted 是否删除
     * @return 满减规则列表
     */
    @Query("SELECT r FROM ThresholdDiscountRule r WHERE r.ruleId IN :ruleIds AND r.isDeleted = :isDeleted")
    List<ThresholdDiscountRule> findByRuleIds(@Param("ruleIds") List<String> ruleIds,
                                             @Param("isDeleted") Boolean isDeleted);
    
    // ========== 金额范围查询 ==========
    
    /**
     * 根据门槛金额范围查询满减规则
     * 
     * @param minThreshold 最小门槛金额
     * @param maxThreshold 最大门槛金额
     * @param isDeleted 是否删除
     * @return 满减规则列表
     */
    @Query("SELECT r FROM ThresholdDiscountRule r WHERE r.thresholdAmount BETWEEN :minThreshold AND :maxThreshold " +
           "AND r.isDeleted = :isDeleted ORDER BY r.thresholdAmount ASC")
    List<ThresholdDiscountRule> findByThresholdAmountRange(@Param("minThreshold") BigDecimal minThreshold,
                                                           @Param("maxThreshold") BigDecimal maxThreshold,
                                                           @Param("isDeleted") Boolean isDeleted);
    
    /**
     * 查询门槛金额小于等于指定金额的满减规则
     * 
     * @param amount 金额
     * @param isDeleted 是否删除
     * @return 满减规则列表
     */
    @Query("SELECT r FROM ThresholdDiscountRule r WHERE r.thresholdAmount <= :amount " +
           "AND r.isDeleted = :isDeleted ORDER BY r.thresholdAmount DESC")
    List<ThresholdDiscountRule> findByThresholdAmountLessThanEqual(@Param("amount") BigDecimal amount,
                                                                   @Param("isDeleted") Boolean isDeleted);
    
    /**
     * 根据减免金额范围查询满减规则
     * 
     * @param minDiscount 最小减免金额
     * @param maxDiscount 最大减免金额
     * @param isDeleted 是否删除
     * @return 满减规则列表
     */
    @Query("SELECT r FROM ThresholdDiscountRule r WHERE r.discountAmount BETWEEN :minDiscount AND :maxDiscount " +
           "AND r.isDeleted = :isDeleted")
    List<ThresholdDiscountRule> findByDiscountAmountRange(@Param("minDiscount") BigDecimal minDiscount,
                                                          @Param("maxDiscount") BigDecimal maxDiscount,
                                                          @Param("isDeleted") Boolean isDeleted);
    
    // ========== 自定义查询 ==========
    
    /**
     * 根据ID查询未删除的满减规则
     * 
     * @param id 满减规则ID
     * @return 满减规则对象
     */
    @Query("SELECT r FROM ThresholdDiscountRule r WHERE r.id = :id AND r.isDeleted = false")
    Optional<ThresholdDiscountRule> findByIdAndNotDeleted(@Param("id") String id);
    
    /**
     * 查询所有适用于指定金额的满减规则（门槛金额 <= 指定金额）
     * 
     * @param amount 购物车金额
     * @param isDeleted 是否删除
     * @return 满减规则列表（按门槛金额降序）
     */
    @Query("SELECT r FROM ThresholdDiscountRule r WHERE r.thresholdAmount <= :amount " +
           "AND r.isDeleted = :isDeleted ORDER BY r.thresholdAmount DESC")
    List<ThresholdDiscountRule> findApplicableRules(@Param("amount") BigDecimal amount,
                                                    @Param("isDeleted") Boolean isDeleted);
    
    /**
     * 查询最大门槛金额的满减规则
     * 
     * @param isDeleted 是否删除
     * @return 满减规则对象
     */
    @Query("SELECT r FROM ThresholdDiscountRule r WHERE r.isDeleted = :isDeleted " +
           "ORDER BY r.thresholdAmount DESC")
    Optional<ThresholdDiscountRule> findByMaxThresholdAmount(@Param("isDeleted") Boolean isDeleted);
    
    /**
     * 统计指定规则的满减配置数量
     * 
     * @param ruleId 规则ID
     * @param isDeleted 是否删除
     * @return 满减配置数量
     */
    Long countByRuleIdAndIsDeleted(String ruleId, Boolean isDeleted);
    
    /**
     * 统计可叠加的满减规则数量
     * 
     * @param stackable 是否可叠加
     * @param isDeleted 是否删除
     * @return 满减规则数量
     */
    Long countByStackableAndIsDeleted(Boolean stackable, Boolean isDeleted);
}

